package com.sxbang.bms.service.impl;

import com.sxbang.bms.base.result.Results;
import com.sxbang.bms.dao.RoleUserDao;
import com.sxbang.bms.dao.UserDao;
import com.sxbang.bms.dto.UserDto;
import com.sxbang.bms.model.SysRoleUser;
import com.sxbang.bms.model.SysUser;
import com.sxbang.bms.service.UserService;
import com.sxbang.bms.util.MD5;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

	private static final Logger log = LoggerFactory.getLogger("adminLogger");

	@Autowired
	private UserDao userDao;
	@Autowired
	private RoleUserDao roleUserDao;

	@Override
	public SysUser getUser(String username) {
		return userDao.getUser(username);
	}

	@Override
	public SysUser getUserById(Long id) {
		return userDao.getById(id);
	}

	@Override
	@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
	public int deleteUser(Long id) {
		roleUserDao.deleteRoleUserByUserId(id.intValue());
		return userDao.deleteUser(id);
	}

	@Override
	public SysUser getUerByUsernameAndPassword(String username, String password) {
		SysUser user = userDao.getUerByUsernameAndPassword(username,password);
		return user ; }

	@Override
	public Results<SysUser> getAllUsersByPage(Integer startPosition,Integer limit) {
		return Results.success(userDao.countAllUsers().intValue(),userDao.getAllUsersByPage(startPosition,limit));
	}

	@Override
	public Integer getUserByFuzzyUserName(String username) {
		return userDao.getUserByFuzzyUserName(username).intValue();
	}

	@Override
	public Results<SysUser> getUserByFuzzyUserNamePage(String username, Integer startPosition, Integer limit) {
		return Results.success(userDao.getUserByFuzzyUserName(username).intValue(),userDao.getUserByFuzzyUserNamePage(username,startPosition,limit));
	}

	@Override
	public Integer getAllUsers() {
		return userDao.countAllUsers().intValue();
	}

	@Override
	@Transactional
	public Results save(SysUser user,Integer roleId) {

		if(roleId != null){
			userDao.save(user);
			SysRoleUser sysRoleUser = new SysRoleUser();
			sysRoleUser.setRoleId(roleId);
			sysRoleUser.setUserId(user.getId().intValue());
			roleUserDao.save(sysRoleUser);
			return Results.success();
		}
		return Results.failure();

	}

	@Override
	@Transactional
	public Results updateUser(UserDto userDto,Integer roleId) {
		if(roleId != null){
			userDao.updateUser(userDto);
			SysRoleUser sysRoleUser = new SysRoleUser();
			sysRoleUser.setUserId(userDto.getId().intValue());
			sysRoleUser.setRoleId(roleId);
			if(roleUserDao.getSysRoleUserByUserId(userDto.getId().intValue())!= null){
				roleUserDao.updateSysRoleUser(sysRoleUser);
			}else{
				roleUserDao.save(sysRoleUser);
			}

			return Results.success();
		}else{
			return Results.failure();
		}
	}

	@Override
	public SysUser getUserByPhone(String phone) {
		return userDao.getUserByPhone(phone);
	}

	@Override
	public SysUser getUserByEmail(String email) {
		return userDao.getUserByEmail(email);
	}

	@Override
	public Results<SysUser> changePassword(String username, String oldPassword, String newPassword) {
		SysUser u = userDao.getUser(username);
		if (u == null) {
			return Results.failure(1,"用户不存在");
		}
		if (!MD5.crypt(oldPassword).equals(u.getPassword())) {
			return Results.failure(1,"旧密码错误");
		}
		userDao.changePassword(u.getId(), MD5.crypt(newPassword));
		return Results.success();
	}

}
